package com.bxd.baodan.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.VideoView;

/**
 * 视图工作类
 * 
 * @author oldfeel
 * 
 *         Created on: 2014年3月1日
 */
public class ViewUtil {
	public static Bitmap createCircleImage(Bitmap bitmap) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getWidth(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getWidth());
		final RectF rectF = new RectF(rect);
		final float roundPx = bitmap.getWidth() / 2;

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	// public static Bitmap createCircleImage(Bitmap source, int min)
	// {
	// final Paint paint = new Paint();
	// paint.setAntiAlias(true);
	// Bitmap target = Bitmap.createBitmap(source.getWidth(),
	// source.getHeight(), Config.ARGB_8888);
	// /**
	// * 产生一个同样大小的画布
	// */
	// Canvas canvas = new Canvas(target);
	// /**
	// * 首先绘制圆形
	// */
	// canvas.drawCircle(min / 2, min / 2, min / 2, paint);
	// /**
	// * 使用SRC_IN
	// */
	// paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
	// /**
	// * 绘制图片
	// */
	// canvas.drawBitmap(source, 0, 0, paint);
	// return target;
	// }
	public static GridView getGridView(View view, int id) {
		return (GridView) view.findViewById(id);
	}

	public static TextView getTextView(View view, int id) {
		return (TextView) view.findViewById(id);
	}

	public static Button getButton(View view, int id) {
		return (Button) view.findViewById(id);
	}

	public static ImageView getImageView(View view, int id) {
		return (ImageView) view.findViewById(id);
	}

	public static LinearLayout getLinearLayout(View view, int id) {
		return (LinearLayout) view.findViewById(id);
	}

	public static RelativeLayout getRelativeLayout(View view, int id) {
		return (RelativeLayout) view.findViewById(id);
	}

	public static ListView getListView(View view, int id) {
		return (ListView) view.findViewById(id);
	}

	public static VideoView getVideoView(View view, int id) {
		return (VideoView) view.findViewById(id);
	}

	public static EditText getEditText(View view, int id) {
		return (EditText) view.findViewById(id);
	}

	public static ImageButton getImageButton(View view, int id) {
		return (ImageButton) view.findViewById(id);
	}

	public static RatingBar getRatingBar(View view, int id) {
		return (RatingBar) view.findViewById(id);
	}

	public static Spinner getSpinner(View view, int id) {
		return (Spinner) view.findViewById(id);
	}

	public static RadioGroup getRadioGroup(View view, int id) {
		return (RadioGroup) view.findViewById(id);
	}

	public static CheckBox getCheckBox(View view, int id) {
		return (CheckBox) view.findViewById(id);
	}

	public static int getInt(Spinner spinner) {
		String string = getString(spinner);
		if (string == null || string.length() == 0) {
			return 0;
		}
		string = string.replaceAll("[\u4e00-\u9fa5]", "");
		try {
			return Integer.valueOf(string);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static String getString(Spinner spinner) {
		if (spinner.getSelectedItem() == null) {
			return "";
		}
		return spinner.getSelectedItem().toString();
	}

	public static void updateListViewHeight(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

	public static void setDialogSize(Dialog dg) {
		Window dialogWindow = dg.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
		// // 显示的坐标
		// lp.x = 150;
		lp.y = 200;
		// int width = 160;
		// int height = 160;
		// dialog的大小
		// lp.width = width;
		// lp.height = height;
		dialogWindow.setAttributes(lp);
	}

	/**
	 * dip转换像素px
	 */
	public static int dip2px(Context context, float dpValue) {
		try {
			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (dpValue * scale + 0.5f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (int) dpValue;
	}

}
